<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" size="small" label-width="80px">
      <el-form-item label="资源名称：">
        <el-input v-model="listQuery.nameKeyword" class="input-width" placeholder="资源名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="资源路径：">
        <el-input v-model="listQuery.urlKeyword" class="input-width" placeholder="资源路径" clearable></el-input>
      </el-form-item>
      <el-form-item label="资源分类：">
        <el-select v-model="listQuery.categoryId" placeholder="全部" clearable class="input-width">
          <el-option v-for="item in categoryOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearchList" size="small"> 搜索 </el-button>
        <el-button @click="handleResetSearch" size="small"> 重置 </el-button>
        <el-button size="small" class="btn-add" @click="handleAdd">添加</el-button>
        <el-button size="small" class="btn-add" @click="handleShowCategory">资源分类</el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table ref="resourceTable" height="100%" :data="list" style="width: 100%" v-loading="listLoading" border>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="资源名称" min-width="140" align="center">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="资源路径" min-width="140" show-overflow-tooltip align="center">
          <template #default="scope">{{ scope.row.url }}</template>
        </el-table-column>
        <el-table-column label="描述" align="center">
          <template #default="scope">{{ scope.row.description }}</template>
        </el-table-column>
        <el-table-column label="添加时间" width="160" align="center">
          <template #default="scope">{{ formatDateTime(scope.row.createTime) }}</template>
        </el-table-column>
        <el-table-column label="操作" width="140" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="handleUpdate(scope.row)"> 编辑 </el-button>
            <el-button type="danger" size="small" link @click="handleDelete(scope.row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        v-model:current-page="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 15, 20]"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog :title="isEdit ? '编辑资源' : '添加资源'" v-model="dialogVisible" width="40%">
      <el-form :model="resource" ref="resourceForm" label-width="auto" size="small">
        <el-form-item label="资源名称：">
          <el-input v-model="resource.name"></el-input>
        </el-form-item>
        <el-form-item label="资源路径：">
          <el-input v-model="resource.url"></el-input>
        </el-form-item>
        <el-form-item label="资源分类：">
          <el-select v-model="resource.categoryId" placeholder="全部" clearable>
            <el-option v-for="item in categoryOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述：">
          <el-input v-model="resource.description" type="textarea" :rows="5"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="handleDialogConfirm" size="small">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { fetchList, createResource, updateResource, deleteResource } from '@/api/resource'
import { listAllCate } from '@/api/resourceCategory'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import type { Resource, ResourcePageDto } from '@/models/resource'
import type { CommonOptions } from '@/models/common'
import { formatDateTime } from '@/utils/format'

defineOptions({ name: 'ResourceList' })
const router = useRouter()
const listQuery = ref<ResourcePageDto>({ pageNum: 1, pageSize: 10 })
const list = ref<Resource[]>([])
const total = ref(0)
const listLoading = ref(false)
const dialogVisible = ref(false)
const resource = ref<Resource>({})
const isEdit = ref(false)
const categoryOptions = ref<CommonOptions[]>([])
getList()
getCateList()

function handleResetSearch() {
  listQuery.value = { pageNum: 1, pageSize: 10 }
}
function handleSearchList() {
  listQuery.value.pageNum = 1
  getList()
}
function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}
function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}
function handleAdd() {
  dialogVisible.value = true
  resource.value = {}
}
async function handleDelete(row: Resource) {
  await ElMessageBox.confirm('是否要删除该资源?', '提示', { type: 'warning' })
  await deleteResource(row.id as number)
  ElMessage.success('删除成功！')
  getList()
}
function handleUpdate(row: Resource) {
  dialogVisible.value = true
  resource.value = Object.assign({}, row)
}
async function handleDialogConfirm() {
  if (resource.value.id) {
    await updateResource(resource.value.id, resource.value)
    ElMessage.success('添加成功！')
  } else {
    await createResource(resource.value)
    ElMessage.success('添加成功！')
  }
  dialogVisible.value = false
  getList()
}
function handleShowCategory() {
  router.push({ path: '/ums/resource/category' })
}
async function getList() {
  listLoading.value = true
  const response = await fetchList(listQuery.value)
  listLoading.value = false
  list.value = response.data.data.list
  total.value = response.data.data.total
}
async function getCateList() {
  const response = await listAllCate()
  let cateList = response.data.data
  for (let i = 0; i < cateList.length; i++) {
    let cate = cateList[i]
    categoryOptions.value.push({ label: cate.name, value: cate.id })
  }
}
</script>
<style>
.input-width {
  width: 203px;
}
</style>
